package com.ctgu.redislock01.controller;

import com.ctgu.redislock01.service.UsersService;
import com.ctgu.redislock01.utils.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Author Alex2
 * @Date 2025/2/25 10:35
 **/
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UsersService userService;

    /**
     * @Author: Alex
     * @Description: 缓存列表
     */
    // http://localhost:8080/api/users/getAllUser
    @GetMapping("getAllUser")
    public ApiResult getAllUser() {
        return userService.getAllUser();
    }

    /**
     * @Author: Alex
     * @Description: 双写一致-延时双删
     */
    // http://localhost:8080/api/users/updateUser?id=1&username=Alice&password=654321&phone=19186247825
    @GetMapping("updateUser")
    public ApiResult updateUser(@RequestParam(value = "id") Integer id,
                                @RequestParam(value = "username") String username,
                                @RequestParam(value = "password") String password,
                                @RequestParam(value = "phone") String phone) {
        return userService.updateUser(id, username, password, phone);
    }

    /**
     * @Author: Alex
     * @Description: 双写一致-RabbitMQ
     */
    @GetMapping("updateUsers")
    public ApiResult updateUsers(@RequestParam(value = "id") Integer id,
                                 @RequestParam(value = "username") String username,
                                 @RequestParam(value = "password") String password,
                                 @RequestParam(value = "phone") String phone) {
        return userService.updateUsers(id, username, password, phone);
    }

    /**
     * @Author: Alex
     * @Description: 解决缓存穿透-缓存空对象
     */
    // http://localhost:8080/api/users/getUser/-1
    @GetMapping("getUser/{id}")
    public ApiResult getUser(@PathVariable Integer id) {
        return userService.getUser(id);
    }

    /**
     * @Author: Alex
     * @Description: 解决缓存雪崩-随机过期时间
     */
    // http://localhost:8080/api/users/getUserEntity/1
    @GetMapping("getUserEntity/{id}")
    public ApiResult getUserEntity(@PathVariable Integer id) {
        return userService.getUserEntity(id);
    }

    /**
     * @Author: Alex
     * @Description: 解决缓存击穿-互斥锁
     */
    // http://localhost:8080/api/users/fetchUserById/1
    @GetMapping("fetchUserById/{id}")
    public ApiResult fetchUserById(@PathVariable Integer id) {
        return userService.fetchUserById(id);
    }

    /**
     * @Author: Alex
     * @Description: 解决缓存击穿-互斥锁-逻辑过期 + 异步刷新
     */
    // http://localhost:8080/api/users/fetchUserEntity/1
    @GetMapping("fetchUserEntity/{id}")
    public ApiResult fetchUserEntity(@PathVariable Integer id) {
        return userService.fetchUserEntity(id);
    }

    /**
     * @Author: Alex
     * @Description: 使用 Redis 工具类
     */
    // http://localhost:8080/api/users/getRedisUtils/1
    @GetMapping("getRedisUtils/{id}")
    public ApiResult redisUtils(@PathVariable Integer id) {
        return userService.getRedisUtils(id);
    }
}